package com.cy.store.Controller;

import com.cy.store.Service.Ex.AccessDeniedException;
import com.cy.store.Service.Ex.CartNotFoundException;
import com.cy.store.Service.Ex.UpdateException;
import com.cy.store.Service.ICartService;
import com.cy.store.util.JsonResult;
import com.cy.store.vo.CartVO;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/carts")
public class CartController extends BaseController{
    @Autowired
    private ICartService cartService;

    @RequestMapping("/add_to_cart")
    public JsonResult<Void> addToCart(Integer pid,Integer amount,HttpSession session){
//        if(amount == null || amount <= 0){
//            throw new RuntimeException("加入购物车数量不能为空");
//        }
        cartService.addToCart(getuidFromSession(session),pid,amount,getusernameFromSession(session));
        return new JsonResult<>(OK,"添加购物车成功");
    }

    @RequestMapping({"", "/"})
    public JsonResult<List<CartVO>> getVOByUid(HttpSession session) {
        List<CartVO> data = cartService.getVOByUid(getuidFromSession(session));
        return new JsonResult<>(OK, data);
    }


    @RequestMapping("{cid}/num/add")
    public JsonResult<Integer> addNum(@PathVariable("cid") Integer cid, HttpSession session) throws AccessDeniedException, UpdateException, CartNotFoundException {
        Integer data = cartService.addNum(
                cid,
                getuidFromSession(session),
                getusernameFromSession(session));
        return new JsonResult<Integer>(OK, data);
    }

    @RequestMapping("/list")
    public JsonResult<List<CartVO>> getVOByCid(HttpSession session,Integer[] cids) {
        List<CartVO> data = cartService.getVOByCid(getuidFromSession(session),cids);
        return new JsonResult<>(OK, data);
    }

}
